package com.windliven.spoc.modules.init.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.windliven.spoc.modules.init.entity.SysRole;
import com.windliven.spoc.modules.init.entity.SysUser;
import com.windliven.spoc.modules.init.entity.SysUserRole;
import com.windliven.spoc.modules.init.mapper.SysRoleMapper;
import com.windliven.spoc.modules.init.mapper.SysUserMapper;
import com.windliven.spoc.modules.init.mapper.SysUserRoleMapper;
import com.windliven.spoc.modules.init.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author: bysun
 * @Date: 2019/5/26 10:16
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public Set<String> getRoles(String username) {
        SysUser sysUser = new SysUser();
        SysRole sysRole;
        sysUser.setUsername(username);
//        List<SysRole> roles = Lists.newArrayList();
        Set<String> roles = Sets.newHashSet();
        List<SysUser> users = sysUserMapper.select(sysUser);
        if (users.size() > 0) {
            SysUser user = users.get(0);
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(user.getId());
            List<SysUserRole> roleIds = sysUserRoleMapper.select(sysUserRole);
            for (SysUserRole userRole : roleIds) {
                sysRole = sysRoleMapper.selectByPrimaryKey(userRole.getRoleId());
                roles.add(sysRole.getRoleName());
            }
        }
        return null;
    }
}
